﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//请先添加引用
namespace Usas.DAL.Cms
{
    /// <summary>
    /// 数据访问类cmsReview。
    /// </summary>
    public class cmsReview
    {
        public cmsReview()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(long ReviewId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from cmsReview");
            strSql.Append(" where ReviewId=" + ReviewId + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Usas.Model.Cms.cmsReview model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.ArtId != null)
            {
                strSql1.Append("ArtId,");
                strSql2.Append("" + model.ArtId + ",");
            }
            if (model.Reviewer != null)
            {
                strSql1.Append("Reviewer,");
                strSql2.Append("'" + model.Reviewer + "',");
            }
            if (model.Content != null)
            {
                strSql1.Append("Content,");
                strSql2.Append("'" + model.Content + "',");
            }
            if (model.EditTime != null)
            {
                strSql1.Append("EditTime,");
                strSql2.Append("'" + model.EditTime + "',");
            }
            if (model.IsCheck != null)
            {
                strSql1.Append("IsCheck,");
                strSql2.Append("" + (model.IsCheck ? 1 : 0) + ",");
            }
            strSql.Append("insert into cmsReview(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Usas.Model.Cms.cmsReview model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update cmsReview set ");
            if (model.ArtId != null)
            {
                strSql.Append("ArtId=" + model.ArtId + ",");
            }
            if (model.Reviewer != null)
            {
                strSql.Append("Reviewer='" + model.Reviewer + "',");
            }
            if (model.Content != null)
            {
                strSql.Append("Content='" + model.Content + "',");
            }
            if (model.EditTime != null)
            {
                strSql.Append("EditTime='" + model.EditTime + "',");
            }
            if (model.IsCheck != null)
            {
                strSql.Append("IsCheck=" + (model.IsCheck ? 1 : 0) + ",");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ReviewId=" + model.ReviewId + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(long ReviewId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from cmsReview ");
            strSql.Append(" where ReviewId=" + ReviewId + " ");
            DbHelperSQL.ExecuteSql(strSql.ToString());
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Usas.Model.Cms.cmsReview GetModel(long ReviewId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" ReviewId,ArtId,Reviewer,Content,EditTime,IsCheck ");
            strSql.Append(" from cmsReview ");
            strSql.Append(" where ReviewId=" + ReviewId + " ");
            Usas.Model.Cms.cmsReview model = new Usas.Model.Cms.cmsReview();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ReviewId"].ToString() != "")
                {
                    model.ReviewId = long.Parse(ds.Tables[0].Rows[0]["ReviewId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ArtId"].ToString() != "")
                {
                    model.ArtId = long.Parse(ds.Tables[0].Rows[0]["ArtId"].ToString());
                }
                model.Reviewer = ds.Tables[0].Rows[0]["Reviewer"].ToString();
                model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                if (ds.Tables[0].Rows[0]["EditTime"].ToString() != "")
                {
                    model.EditTime = DateTime.Parse(ds.Tables[0].Rows[0]["EditTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsCheck"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsCheck"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsCheck"].ToString().ToLower() == "true"))
                    {
                        model.IsCheck = true;
                    }
                    else
                    {
                        model.IsCheck = false;
                    }
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ReviewId,ArtId,Reviewer,Content,EditTime,IsCheck ");
            strSql.Append(" FROM cmsReview ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ReviewId,ArtId,Reviewer,Content,EditTime,IsCheck ");
            strSql.Append(" FROM cmsReview ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        */

        #endregion  成员方法
    }
}

